package com.ctrip.study.algorithm.link;

public class LinkList {
	
	private Link first;
	
	public Link getFirst() {
		return first;
	}

	public boolean isEmpty(){
		return first == null;
	}
	
	public void insertFirst(Link link){
		link.next = first;
		first = link;
	}
	
	public Link deleteFirst(){
		Link second = first.next;
		first = second;
		return second;
	}
	
	public Link find(Link link){
		Link temp = first;
		while(temp != null){
			if(temp.equals(link)){
				return temp;
			}
			temp = temp.next;
		}
		return null;
	}
	
	
	public void delete(Link link){
		Link current = first;
		Link previous = first;
		while(current != null){
			if(current.equals(link)){
				if(current == first){
					first = current.next;
				}else{
					previous.next = current.next;
				}
			}
			previous = current;
			current = current.next;
		}
	}
	
	
	
	public void display(){
		Link currentLink = first;
		while(currentLink != null){
			currentLink.displayLink();
			currentLink = currentLink.next;
		}
	}

}
